<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.username"
        placeholder="搜索用户名"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.nickname"
        placeholder="搜索昵称"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="用户名">
        <template slot-scope="scope">
          <span>{{ scope.row.username }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="昵称">
        <template slot-scope="scope">
          <span>{{ scope.row.nickname }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="状态">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 0 ? 'success' : 'danger'">
            {{ scope.row.status === 0 ? '正常' : '封禁' }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column width="180px" align="center" label="注册时间">
        <template slot-scope="scope">
          <span>{{ new Date(scope.row.createTime).getTime() | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="发帖数">
        <template slot-scope="scope">
          <span>{{ scope.row.postCount || 0 }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="评论数">
        <template slot-scope="scope">
          <span>{{ scope.row.commentCount || 0 }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status === 0"
            size="mini"
            type="danger"
            @click="handleBan(scope.row)"
          >
            封禁
          </el-button>
          <el-button
            v-else
            size="mini"
            type="success"
            @click="handleUnban(scope.row)"
          >
            解封
          </el-button>
          <el-button
            size="mini"
            type="primary"
            @click="handleViewPosts(scope.row)"
          >
            查看帖子
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
// 注意：这里需要根据实际的用户API进行调整
// 假设我们有一个获取用户列表的API
import { getUserList } from '@/api/user'
import { banUser, unbanUser } from '@/api/admin'
import Pagination from '@/components/Pagination'
import waves from '@/directive/waves'
import { parseTime } from '@/utils'

export default {
  name: 'UserList',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        username: undefined,
        nickname: undefined
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      try {
        // 这里需要根据实际的用户API进行调整
        const response = await getUserList()
        this.list = response.data
        this.total = this.list.length
      } catch (error) {
        console.error('获取用户列表失败:', error)
     
      } finally {
        this.listLoading = false
      }
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleBan(user) {
      this.$confirm('确定要封禁这个用户吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await banUser(user.id)
          this.$message({
            type: 'success',
            message: '封禁成功!'
          })
          this.getList()
        } catch (error) {
          console.error('封禁用户失败:', error)
        }
      })
    },
    handleUnban(user) {
      this.$confirm('确定要解封这个用户吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await unbanUser(user.id)
          this.$message({
            type: 'success',
            message: '解封成功!'
          })
          this.getList()
        } catch (error) {
          console.error('解封用户失败:', error)
        }
      })
    },
    handleViewPosts(user) {
      // 跳转到该用户的帖子列表
      this.$router.push(`/forum/user-posts/${user.id}`)
    }
  }
}
</script>

<style scoped>
.filter-container {
  margin-bottom: 20px;
}
.filter-item {
  margin-right: 10px;
}
</style>